import wx
from ZoomHelper import ZoomHelper
from Renderer import Renderer
from MouseHandler import MouseHandler
from BruTile.Extent import Extent


class MapControl(object):

    __map_buffer = None
    tile_layer = None
    map_transform = None
    __render_bb = False
    __bb_extent = Extent(0,0,1,1)
    setTileSeedExtent1 = None
    tile_seed_extent = None
    mouse_handler = None
    __renderer = Renderer()
    
    inProcess = False
    
    __layerExtent = None
    __seedExtent = None
    
    updateSeedInfo = None
    
    def __init__(self):
        self.mouse_handler = MouseHandler(self)
    
    def clearExtents(self):
        self.__layerExtent = None
        self.__seedExtent = None
        self.refresh()
    
    def setBBExtent(self, extent):
        self.__bb_extent = extent
        self.__render_bb = True
    
    def setTileSeedExtent(self, extent):
        self.tile_seed_extent = extent
        self.refresh()
        
    def getMapTransform(self):
        return self.map_transform
    
    def setMapTransform(self, map_transform):
        self.map_transform = map_transform
    
    def getTileLayer(self):
        return self.tile_layer
    
    def setTileLayer(self, tile_layer):
        self.tile_layer = tile_layer
    
    def draw(self, dc, window_width, window_height):
        tiles = self.tile_layer.tile_schema.getTilesInView1(self.map_transform.extent, self.map_transform.resolution)
        empty_map = wx.EmptyBitmap(window_width, window_height)
        mdc = wx.MemoryDC(empty_map)
        for tile in tiles:
            image = self.tile_layer.memory_cache.find(tile.key)
            
            if not(image is None):
                self.__renderer.draw(mdc, image, tile, self.map_transform)
                    
        if not(self.__layerExtent is None):
            self.__renderer.renderExtent('red', mdc, self.map_transform, self.__layerExtent)
            if not(self.__seedExtent is None):
                #if not self.inProcess:
                self.__renderer.renderExtent('green', mdc, self.map_transform, self.__seedExtent)
                    
        dc.Blit(0,0,window_width, window_height,mdc,0,0)
    
    ''' 
    def zoomToBoundingBox(self, event):
        #if not self.__seedExtent is None and not self.__layerExtent is None:
        #    self.__seedExtent = self.__layerExtent.intersectsWith(self.__seedExtent)
        #self.refresh()
        #self.setTileSeedExtent1(self.__bb_extent)
        
        self.__bb_extent = MapTransformHelpers.recalculateBBExtent(self.__bb_extent, self.tile_layer.tile_schema)
        resolution = ZoomHelper.zoomToBoudingBox(self, self.__bb_extent, 1024, self.map_transform.getResolution())
        resolution = ZoomHelper.findNearestResolution(self.tile_layer.tile_schema.resolutions,
                                                                resolution)
        self.map_transform.setResolution(resolution)
        
        self.map_transform.setCenter(Pointf(self.__bb_extent.getCenterX(), 
                                         self.__bb_extent.getCenterY()))
        self.refresh();
        '''
    
    def setLayerExtent(self, extent):
        self.__layerExtent = extent
        self.refresh()
    
    def updateProperties(self, extent):
        #if not self.updateSeedInfo is None and not self.inProcess:
        if not self.updateSeedInfo is None:
            refresh = self.updateSeedInfo(self.__seedExtent)
            if refresh and not refresh is None:
                self.setSeedExtent(extent)
    
    def setSeedExtent(self, extent):
        if not self.__layerExtent is None:
            self.__seedExtent = extent.intersectsWith(self.__layerExtent)
            self.refresh()
        elif self.__seedExtent is None:
            self.__seedExtent = extent
            self.refresh()
    
    def zoomIn(self, mouse_map_pos): 
        
        mouse_map_pos.x = mouse_map_pos.x
        mouse_map_pos.y = mouse_map_pos.y
        mouse_map_pos = self.map_transform.posWithinMap(mouse_map_pos.x, mouse_map_pos.y)

        self.map_transform.setCenter(self.map_transform.mapToWorld
                                        (mouse_map_pos.x, mouse_map_pos.y))
        self.map_transform.setResolution(ZoomHelper.zoomIn(self.tile_layer.tile_schema.resolutions,
                                                                self.map_transform.resolution))
        self.map_transform.setCenter(self.map_transform.mapToWorld
                                        (self.map_transform.width - mouse_map_pos.x,
                                         self.map_transform.height - mouse_map_pos.y))
        self.refresh();
        
    def zoomOut(self, mouse_map_pos):
        mouse_map_pos = self.map_transform.posWithinMap(mouse_map_pos.x, mouse_map_pos.y)

        self.map_transform.setCenter(self.map_transform.mapToWorld
                                        (mouse_map_pos.x, mouse_map_pos.y))
        self.map_transform.setResolution(ZoomHelper.zoomOut(self.tile_layer.tile_schema.resolutions,
                                                                self.map_transform.resolution))
        self.map_transform.setCenter(self.map_transform.mapToWorld
                                        (self.map_transform.width - mouse_map_pos.x,
                                         self.map_transform.height - mouse_map_pos.y))
        self.refresh();
    
    def refresh(self):
        self.tile_layer.tile_fetcher.viewChanged(self.map_transform.extent, self.map_transform.resolution)
        self.super_refresh(self.map_transform.extent)